# 如下为我们进行某项实验获得的一些实验数据：
#
# 输入	输出
# 1	4.8
# 2	8.5
# 3	10.4
# 6	21
# 8	25.3

import tensorflow as tf
W = tf.Variable([0.1], dtype=tf.float32, name="W")
b = tf.Variable([-0.1], dtype=tf.float32, name='b')

x = tf.placeholder(tf.float32, name='x')
y = tf.placeholder(tf.float32, name="y")

linear_model = W*x + b
loss_model = tf.reduce_sum(tf.square(linear_model - y))

sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)

# print(sess.run(loss_model, {x:[1, 2, 3, 6, 8], y:[4.8, 8.5, 10.4, 21, 25.3]}))
optimizer = tf.train.GradientDescentOptimizer(0.001)
train = optimizer.minimize(loss_model)

x_train = [1, 2, 3, 6, 8]
y_train = [4.8, 8.5, 10.4, 21.0, 25.3]

for  i in range(10000):
    sess.run(train, {x: x_train, y:y_train})

print('W: %s b: %s loss: %s' % (sess.run(W), sess.run(b), sess.run(loss_model, {x: x_train , y: y_train})))

